﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for JLTVR04DOC.xaml
    /// </summary>
    public partial class JLTVR04DOC : DocumentBase
    {
        #region-------------Properties-----------------------
        string Mandant = "00";
        ApplicationBase ApplicationBase;
        bool IsSetUp = false;
        INTEXDBC INTEXDBC;
        public OracleConnection OracleDB;
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        JLTVR04DOCData m_Data, m_DataX;
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)this.TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)this.TryFindResource("SetProd");
            }
        }
        DataSet SetRpt = new DataSet();
        CollectionViewSource vwVKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwVKPOS");
            }
        }
        DataRow dwVKPOS
        {
            get
            {
                if (vwVKPOS.View == null)
                    return null;
                if ((DataRowView)vwVKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVKPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwVRPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwVRPOS");
            }
        }
        DataRow dwVRPOS
        {
            get
            {
                if (vwVRPOS.View == null)
                    return null;
                if ((DataRowView)vwVRPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVRPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwVRKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwVRKOPF");
            }
        }
        DataRow dwVRKOPF
        {
            get
            {
                if (vwVRKOPF.View == null)
                    return null;
                if ((DataRowView)vwVRKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwVRKOPF.View.CurrentItem).Row;
            }
        }
        public DataSet dsParamaters;
        JLTVR04DOCReport m_rpt;
        string PrintType = "";
        DataRow LastRow = null;
        #endregion
        public JLTVR04DOC()
        {
            InitializeComponent();
            this.Name = "发票打印";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    this.dicParamater = dicParamater;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (!IsSetUp)
            {
                ApplicationBase.Quit();
            }
            base.OnClosing(e); 
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsSetUp)
                {
                    ApplicationBase = new ApplicationBase();
                    ApplicationBase.Load();
                    dicParamater = ApplicationBase.dicParamater;
                }
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                Mandant = (string)dicParamater["MANDANT"];

                m_Data = new JLTVR04DOCData(OracleDB, proxy);
                INTEXDBC = new INTEX.DataBase.Tools.INTEXDBC(OracleDB, proxy);
                #region============TESTINGDATA================
                txtKRS.Text = "J3";
                txtLFD.Text = "002007";
                #endregion 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }

     
        private void btnGetData_Click(object sender, RoutedEventArgs e)
        {
            if (dwVRKOPF == null)
            {
                MessageBox.Show("请选择发票号码!");
            }
            try
            {
                DataRow dwData = SetData.VRPOS.NewRow();
                dwData["VRPMANDANT"] = Mandant;
                dwData["VRPAUFKRS"] = m_Data.Left(txtKRS.Text.ToString().Trim() + "  ", 2);
                dwData["VRPAUFLFD"] = m_Data.Right("000000" + txtLFD.Text.ToString(), 6);
                dwData["VRPRENR"] = dwVRKOPF["VRKRENR"];
                DataSet setResult = m_Data.GetData(dwData);
                SetRpt = new DataSet();
                SetRpt = setResult;
                SetProd.Clear();
                SetProd.Merge(setResult.Tables["VRPOS"]);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void GenerateReport(string PrintType)
        {
            try
            {
                JLTVR04DOCReport m_rpt = new JLTVR04DOCReport(dicParamater);
                JLTVR04DOCData m_DX = new JLTVR04DOCData(dicParamater);
                m_rpt.dsSource = SetRpt;
                m_rpt.PrintType = PrintType;
                m_rpt.exAgent = m_DX.exAgent;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.proxy = this.proxy;
                m_rpt.WriteExcelReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintType = "DirectPrint";
            GenerateReport(PrintType);
        }

        private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
        {
            PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetData.AcceptChanges();
                DataRow dwData = SetData.VRPOS.NewRow();
                dwData["VRPMANDANT"] = Mandant;
                dwData["VRPAUFKRS"] = m_Data.Left(txtKRS.Text.ToString().Trim() + "  ", 2);
                dwData["VRPAUFLFD"] = m_Data.Right("000000" + txtLFD.Text.ToString(), 6);
                DataSet SResult = m_Data.BuildData(dwData);
                SetData.Clear();
                SetData.AcceptChanges();
                SetData.Merge(SResult);
                if (vwVRKOPF.View != null)
                {
                    vwVRKOPF.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
    }
}
